﻿using AbpVnext_Module.Model;
using AbpVnext_Module.Repository;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.IdentityModel.JsonWebTokens;
using System.Security.Claims;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Users;

namespace AbpVnext_Module.Permission
{
    /// <summary>
    /// 鉴定权限类
    /// </summary>
    public class CustomPermissionValueProvider : PermissionValueProvider
    {
        public IHttpContextAccessor HttpContext { get; set; }

        public IPermissionGrantsRepository PermissionGrants { get; set; }

        public CustomPermissionValueProvider(IPermissionStore permissionStore) : base(permissionStore)
        {

        }

        public const string ProviderName = "username";

        /// <summary>
        /// 基于用户名鉴权
        /// </summary>
        public override string Name => "username";

        /// <summary>
        /// 实现鉴权
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public override async Task<PermissionGrantResult> CheckAsync(PermissionValueCheckContext context)
        {
            #region 持久化鉴权
            {
                //throw new NotImplementedException();
                if (HttpContext.HttpContext.User.Identity?.IsAuthenticated == false)
                    return PermissionGrantResult.Undefined;
                //获取Cookie中的用户
                ClaimsPrincipal claimsPrincipal = context.Principal;
                //获取Product_Select权限
                Claim claim = claimsPrincipal.Claims.Where(x => x.Type == JwtRegisteredClaimNames.Name).First();
                List<PermissionGrants> permissionGrants = await PermissionGrants.GetListAsync();
                var userList = permissionGrants.Where(x => x.ProviderName == Name && x.ProviderKey == claim.Value).ToList();
                //获取接口上的权限
                string PermissionName = context.Permission.Name;
                //if (PermissionName == "Product_Select_Page")
                //    return PermissionGrantResult.Undefined;
                //鉴定用户是否有权限
                if (userList.Any(x => x.PermissionName == PermissionName))
                {
                    //授予权限
                    //return PermissionGrantResult.Prohibited;
                    return PermissionGrantResult.Granted;
                    //return PermissionGrantResult.Prohibited;
                }
                else
                {
                    //鉴权失败
                    return PermissionGrantResult.Undefined;
                }

                //return PermissionGrantResult.Undefined;
            }
            #endregion

            #region  内存鉴权
            {
                ////throw new NotImplementedException();
                //if (HttpContext.HttpContext.User.Identity?.IsAuthenticated == false)
                //    return PermissionGrantResult.Undefined;
                ////获取Cookie中的用户
                //ClaimsPrincipal claimsPrincipal = context.Principal;
                ////获取Product_Select权限
                //Claim claim = claimsPrincipal.Claims.Where(x => x.Type == "UserRole").First();
                //List<string> strings = JsonConvert.DeserializeObject<List<string>>(claim.Value.ToString());
                ////获取接口上的权限
                //string PermissionName = context.Permission.Name;
                ////鉴权
                //if (strings.Any(x => x == PermissionName))
                //{
                //    //授予权限
                //    //return PermissionGrantResult.Prohibited;
                //    return PermissionGrantResult.Granted;
                //    //return PermissionGrantResult.Prohibited;
                //}
                //else
                //{
                //    //鉴权失败
                //    return PermissionGrantResult.Undefined;
                //}
            }
            #endregion
        }

        public override Task<MultiplePermissionGrantResult> CheckAsync(PermissionValuesCheckContext context)
        {
            throw new NotImplementedException();
        }
    }
}
